package com.hibernate.rcache.region;

import com.hibernate.rcache.RcacheAccessStrategyFactory;
import com.rcache.core.cache.Rcache;
import com.rcache.core.manager.CacheManager;
import org.hibernate.cache.CacheDataDescription;
import org.hibernate.cache.CacheException;
import org.hibernate.cache.CollectionRegion;
import org.hibernate.cache.Timestamper;
import org.hibernate.cache.access.AccessType;
import org.hibernate.cache.access.CollectionRegionAccessStrategy;
import org.hibernate.cfg.Settings;

import java.util.Map;
import java.util.Properties;

public class RcacheCollectionRegion implements CollectionRegion {
    private static final String CACHE_LOCK_TIMEOUT_PROPERTY = "";
    private static final int DEFAULT_CACHE_LOCK_TIMEOUT = 60000;
    private final int cacheLockTimeout;
    private final Settings settings;
    private final CacheDataDescription metadata;
    private final CacheManager cacheManager;
    private final Rcache cache;
    RcacheAccessStrategyFactory accessStrategyFactory ;
    public RcacheCollectionRegion(CacheManager cacheManager, Rcache cache, Properties properties, Settings settings, CacheDataDescription metadata) {
        this.cacheManager = cacheManager;
        this.cache = cache;
        this.settings = settings;
        this.metadata = metadata;
        String timeout = properties.getProperty(
                CACHE_LOCK_TIMEOUT_PROPERTY,
                Integer.toString( DEFAULT_CACHE_LOCK_TIMEOUT )
        );
        this.cacheLockTimeout = Timestamper.ONE_MS * Integer.decode( timeout );
    }



    public boolean isTransactionAware() {
        return false;
    }

    public CacheDataDescription getCacheDataDescription() {
        return metadata;
    }


    public String getName() {
        return cache.getCacheName();
    }

    public void destroy() throws CacheException {
        cacheManager.remove(cache.getCacheName());
    }

    public boolean contains(Object key) {
        return cache.containsKey(key);
    }

    public long getSizeInMemory() {
        return 0;
    }

    public long getElementCountInMemory() {
        return 0;
    }

    public long getElementCountOnDisk() {
        return 0;
    }

    public Map toMap() {
        return cache.toMap();
    }

    public long nextTimestamp() {
        return Timestamper.next();
    }

    public int getTimeout() {
        return 0;
    }

    public Settings getSettings() {
        return settings;
    }

    public CacheDataDescription getMetadata() {
        return metadata;
    }

    public CollectionRegionAccessStrategy buildAccessStrategy(AccessType accessType) throws CacheException {
        return null;
    }
}
